

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/HexoPage/img/favicon.jpeg">
  <link rel="icon" href="/HexoPage/img/favicon.jpeg">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="xiaoaodi">
  <meta name="keywords" content="">
  
    <meta name="description" content="#目录[TOC] 三、面向对象高级1. 对象创建模式1.1 Object构造函数模式方式一：先创建空Object对象，再动态的添加属性和方法 适合的场景：开始时不确定对象内部的属性 问题：程序冗余度高 12345678910111213141516&#x2F;*一个人: name:&quot;Tom&quot;, age: 12*&#x2F;&#x2F;&#x2F; 先创建空Object对象,两种创建的方式 var p &#x3D; new Ob">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript学习巩固基础外加进阶(下)底层原理">
<meta property="og:url" content="http://xiao_aodi.gitee.io/hexopage/2022/03/23/JavaScript%E5%AD%A6%E4%B9%A0%E5%B7%A9%E5%9B%BA%E5%9F%BA%E7%A1%80%E5%A4%96%E5%8A%A0%E8%BF%9B%E9%98%B6(%E4%B8%8B)%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%86/index.html">
<meta property="og:site_name" content="~吃椰子灰的胖丁">
<meta property="og:description" content="#目录[TOC] 三、面向对象高级1. 对象创建模式1.1 Object构造函数模式方式一：先创建空Object对象，再动态的添加属性和方法 适合的场景：开始时不确定对象内部的属性 问题：程序冗余度高 12345678910111213141516&#x2F;*一个人: name:&quot;Tom&quot;, age: 12*&#x2F;&#x2F;&#x2F; 先创建空Object对象,两种创建的方式 var p &#x3D; new Ob">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2022-03-23T03:30:00.000Z">
<meta property="article:modified_time" content="2022-03-23T02:09:18.631Z">
<meta property="article:author" content="xiaoaodi">
<meta name="twitter:card" content="summary_large_image">
  
  
  <title>JavaScript学习巩固基础外加进阶(下)底层原理 - ~吃椰子灰的胖丁</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/HexoPage/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"xiao_aodi.gitee.io","root":"/HexoPage/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":false,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/HexoPage/local-search.xml"};
  </script>
  <script  src="/HexoPage/js/utils.js" ></script>
  <script  src="/HexoPage/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 6.1.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/HexoPage/">
      <strong>Xiao_aodi</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>
   
      <!-- 天气网 -->
      


    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/HexoPage/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/HexoPage/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/HexoPage/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/HexoPage/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/HexoPage/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/HexoPage/img/wallhaven-wqve97.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="JavaScript学习巩固基础外加进阶(下)底层原理">
              
            </span>
            
              <div class="mt-3">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-author" aria-hidden="true"></i>
      xiaoaodi
    </span>
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2022-03-23 11:30" pubdate>
        2022年3月23日 中午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      15k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      122 分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">JavaScript学习巩固基础外加进阶(下)底层原理</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：2022年3月23日 上午
                
              </p>
            
            <div class="markdown-body">
              <h1 id="目录"><a href="#目录" class="headerlink" title="#目录"></a>#目录</h1><p>[TOC]</p>
<h1 id="三、面向对象高级"><a href="#三、面向对象高级" class="headerlink" title="三、面向对象高级"></a>三、面向对象高级</h1><h2 id="1-对象创建模式"><a href="#1-对象创建模式" class="headerlink" title="1. 对象创建模式"></a>1. 对象创建模式</h2><h3 id="1-1-Object构造函数模式"><a href="#1-1-Object构造函数模式" class="headerlink" title="1.1 Object构造函数模式"></a>1.1 Object构造函数模式</h3><p>方式一：先创建空Object对象，再动态的添加属性和方法</p>
<p>适合的场景：开始时不确定对象内部的属性</p>
<p>问题：程序冗余度高</p>
<figure class="highlight js"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></div></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">/*一个人: name:&quot;Tom&quot;, age: 12*/</span><br><span class="hljs-comment">// 先创建空Object对象,两种创建的方式</span><br> <span class="hljs-keyword">var</span> p = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Object</span>()<br> p = &#123;&#125; <span class="hljs-comment">//此时内部数据是不确定的</span><br><br> <span class="hljs-comment">// 再动态添加属性/方法</span><br> p.<span class="hljs-property">name</span> = <span class="hljs-string">&#x27;Tom&#x27;</span><br> p.<span class="hljs-property">age</span> = <span class="hljs-number">12</span><br> p.<span class="hljs-property">setName</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">name</span>) &#123;<br>   <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name<br> &#125;<br><br> <span class="hljs-comment">//测试</span><br> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(p.<span class="hljs-property">name</span>, p.<span class="hljs-property">age</span>)<br> p.<span class="hljs-title function_">setName</span>(<span class="hljs-string">&#x27;Bob&#x27;</span>)<br> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(p.<span class="hljs-property">name</span>, p.<span class="hljs-property">age</span>)<br></code></pre></td></tr></table></figure>

<h3 id="1-2-对象字面量模式"><a href="#1-2-对象字面量模式" class="headerlink" title="1.2 对象字面量模式"></a>1.2 对象字面量模式</h3><p>方式二: 对象字面量模式</p>
<p>套路: 使用{}创建对象, 同时指定属性&#x2F;方法</p>
<p>适用场景: 起始时对象内部数据是确定的</p>
<p>问题: 如果创建多个对象, 有重复代码</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">//对象字面量模式</span><br><span class="hljs-keyword">var</span> p = &#123;<br>   <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;Tom&#x27;</span>,<br>   <span class="hljs-attr">age</span>: <span class="hljs-number">12</span>,<br>   <span class="hljs-attr">setName</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params">name</span>) &#123;<br>     <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name<br>   &#125;<br> &#125;<br> <span class="hljs-comment">//测试</span><br> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(p.<span class="hljs-property">name</span>, p.<span class="hljs-property">age</span>)<br> p.<span class="hljs-title function_">setName</span>(<span class="hljs-string">&#x27;JACK&#x27;</span>)<br> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(p.<span class="hljs-property">name</span>, p.<span class="hljs-property">age</span>)<br><br> <span class="hljs-keyword">var</span> p2 = &#123;  <span class="hljs-comment">//如果创建多个对象代码很重复</span><br>   <span class="hljs-attr">name</span>: <span class="hljs-string">&#x27;Bob&#x27;</span>,<br>   <span class="hljs-attr">age</span>: <span class="hljs-number">13</span>,<br>   <span class="hljs-attr">setName</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params">name</span>) &#123;<br>     <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name<br>   &#125;<br> &#125;<br></code></pre></td></tr></table></figure>

<h3 id="1-3-工厂模式"><a href="#1-3-工厂模式" class="headerlink" title="1.3 工厂模式"></a>1.3 工厂模式</h3><p>方式三: 工厂模式</p>
<p>套路: 通过工厂函数动态创建对象并返回</p>
<p>适用场景: 需要创建多个对象</p>
<p>问题: <code>对象没有一个具体的类型</code>, 都是Object类型</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">//返回一个对象的函数===&gt;工厂函数</span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">createPerson</span>(<span class="hljs-params">name, age</span>) &#123; <br> <span class="hljs-keyword">var</span> obj = &#123;<br>   <span class="hljs-attr">name</span>: name,<br>   <span class="hljs-attr">age</span>: age,<br>   <span class="hljs-attr">setName</span>: <span class="hljs-keyword">function</span> (<span class="hljs-params">name</span>) &#123;<br>     <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name<br>   &#125;<br> &#125;<br> <span class="hljs-keyword">return</span> obj<br>&#125;<br><br><span class="hljs-comment">// 创建2个人</span><br><span class="hljs-keyword">var</span> p1 = <span class="hljs-title function_">createPerson</span>(<span class="hljs-string">&#x27;Tom&#x27;</span>, <span class="hljs-number">12</span>)<br><span class="hljs-keyword">var</span> p2 = <span class="hljs-title function_">createPerson</span>(<span class="hljs-string">&#x27;Bob&#x27;</span>, <span class="hljs-number">13</span>)<br><br><span class="hljs-comment">// p1/p2是Object类型</span><br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">createStudent</span>(<span class="hljs-params">name, price</span>) &#123;<br> <span class="hljs-keyword">var</span> obj = &#123;<br>   <span class="hljs-attr">name</span>: name,<br>   <span class="hljs-attr">price</span>: price<br> &#125;<br> <span class="hljs-keyword">return</span> obj<br>&#125;<br><span class="hljs-keyword">var</span> s = <span class="hljs-title function_">createStudent</span>(<span class="hljs-string">&#x27;张三&#x27;</span>, <span class="hljs-number">12000</span>)<br><span class="hljs-comment">// s也是Object</span><br></code></pre></td></tr></table></figure>

<h3 id="1-4-自定义构造函数模式"><a href="#1-4-自定义构造函数模式" class="headerlink" title="1.4 自定义构造函数模式"></a>1.4 自定义构造函数模式</h3><p>方式四: 自定义构造函数模式</p>
<p>套路: 自定义构造函数, 通过new创建对象</p>
<p>适用场景: 需要创建多个<code>类型确定</code>的对象,与上方工厂模式有所对比</p>
<p>问题: 每个对象都有相同的数据, 浪费内存</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">//定义类型</span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params">name, age</span>) &#123;<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">age</span> = age<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">setName</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">name</span>) &#123;<br>   <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name<br> &#125;<br>&#125;<br><span class="hljs-keyword">var</span> p1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>(<span class="hljs-string">&#x27;Tom&#x27;</span>, <span class="hljs-number">12</span>)<br>p1.<span class="hljs-title function_">setName</span>(<span class="hljs-string">&#x27;Jack&#x27;</span>)<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(p1.<span class="hljs-property">name</span>, p1.<span class="hljs-property">age</span>)<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(p1 <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">Person</span>)<br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Student</span> (name, price) &#123;<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">price</span> = price<br>&#125;<br><span class="hljs-keyword">var</span> s = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Student</span>(<span class="hljs-string">&#x27;Bob&#x27;</span>, <span class="hljs-number">13000</span>)<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(s <span class="hljs-keyword">instanceof</span> <span class="hljs-title class_">Student</span>)<br><br><span class="hljs-keyword">var</span> p2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>(<span class="hljs-string">&#x27;JACK&#x27;</span>, <span class="hljs-number">23</span>)<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(p1, p2)<br></code></pre></td></tr></table></figure>

<h3 id="1-5-构造函数-原型的组合模式"><a href="#1-5-构造函数-原型的组合模式" class="headerlink" title="1.5 构造函数+原型的组合模式"></a>1.5 构造函数+原型的组合模式</h3><p>方式五: 构造函数+原型的组合模式–&gt;<code>最好用这个写法</code></p>
<p>套路: 自定义构造函数, 属性在函数中初始化, 方法添加到原型上</p>
<p>适用场景: 需要<code>创建多个类型确定</code>的对象</p>
<p>放在原型上可以节省空间(只需要加载一遍方法)</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">//在构造函数中只初始化一般函数</span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params">name, age</span>) &#123; <br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">age</span> = age<br>&#125;<br><span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">setName</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">name</span>) &#123;<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name<br>&#125;<br><br><span class="hljs-keyword">var</span> p1 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>(<span class="hljs-string">&#x27;Tom&#x27;</span>, <span class="hljs-number">23</span>)<br><span class="hljs-keyword">var</span> p2 = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>(<span class="hljs-string">&#x27;Jack&#x27;</span>, <span class="hljs-number">24</span>)<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(p1, p2)<br></code></pre></td></tr></table></figure>

<h2 id="2、继承模式"><a href="#2、继承模式" class="headerlink" title="2、继承模式"></a>2、继承模式</h2><h3 id="2-1-原型链继承"><a href="#2-1-原型链继承" class="headerlink" title="2.1 原型链继承"></a>2.1 原型链继承</h3><p>方式1: 原型链继承</p>
<ol>
<li>套路<ul>
<li>定义父类型构造函数</li>
<li>给父类型的原型添加方法</li>
<li>定义子类型的构造函数</li>
<li>创建父类型的对象赋值给子类型的原型</li>
<li><code>将子类型原型的构造属性设置为子类型</code>-</li>
<li>给子类型原型添加方法</li>
<li>创建子类型的对象: 可以调用父类型的方法</li>
</ul>
</li>
<li>关键<ul>
<li><code>子类型的原型为父类型的一个实例对象</code></li>
</ul>
</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">//父类型</span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Supper</span>(<span class="hljs-params"></span>) &#123;<br><span class="hljs-variable language_">this</span>.<span class="hljs-property">supProp</span> = <span class="hljs-string">&#x27;父亲的原型链&#x27;</span><br>&#125;<br><span class="hljs-comment">//给父类型的原型上增加一个[showSupperProp]方法,打印自身subProp</span><br><span class="hljs-title class_">Supper</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">showSupperProp</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) &#123;<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">supProp</span>)<br>&#125;<br><br><span class="hljs-comment">//子类型</span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Sub</span>(<span class="hljs-params"></span>) &#123;<br><span class="hljs-variable language_">this</span>.<span class="hljs-property">subProp</span> = <span class="hljs-string">&#x27;儿子的原型链&#x27;</span><br>&#125;<br><br><span class="hljs-comment">// 子类型的原型为父类型的一个实例对象</span><br><span class="hljs-title class_">Sub</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Supper</span>()<br><span class="hljs-comment">// 让子类型的原型的constructor指向子类型</span><br><span class="hljs-comment">// 如果不加,其构造函数找的[`new Supper()`]时从顶层Object继承来的构造函数,指向[`Supper()`]</span><br><span class="hljs-title class_">Sub</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">constructor</span> = <span class="hljs-title class_">Sub</span><br><span class="hljs-comment">//给子类型的原型上增加一个[showSubProp]方法,打印自身subProp</span><br><span class="hljs-title class_">Sub</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">showSubProp</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) &#123;<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable language_">this</span>.<span class="hljs-property">subProp</span>)<br>&#125;<br><br><span class="hljs-keyword">var</span> sub = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Sub</span>()<br><br>sub.<span class="hljs-title function_">showSupperProp</span>() <span class="hljs-comment">//父亲的原型链</span><br>sub.<span class="hljs-title function_">showSubProp</span>() <span class="hljs-comment">//儿子的原型链</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(sub)  <br><span class="hljs-comment">/**</span><br><span class="hljs-comment">Sub &#123;subProp: &quot;儿子的原型链&quot;&#125;</span><br><span class="hljs-comment">subProp: &quot;儿子的原型链&quot;</span><br><span class="hljs-comment">__proto__: Supper</span><br><span class="hljs-comment">constructor: ƒ Sub()</span><br><span class="hljs-comment">showSubProp: ƒ ()</span><br><span class="hljs-comment">supProp: &quot;父亲的原型链&quot;</span><br><span class="hljs-comment">__proto__: Object</span><br><span class="hljs-comment">*/</span><br></code></pre></td></tr></table></figure>

<p><strong>对于代码中[<code>Sub.prototype.constructor = Sub</code>]是否有疑惑?</strong></p>
<p>如果不加,其构造函数找的[<code>new Supper()</code>]是从顶层Object继承来的构造函数,指向[<code>Supper()</code>],虽然如果你不加这句话,大体上使用是不受影响的,但是你有一个属性指向是错误的,如果在大型项目中万一万一哪里再调用到了呢?</p>
<ol>
<li>这里可以补充一下constructor 的概念：</li>
</ol>
<ul>
<li><code>constructor 我们称为构造函数，因为它指回构造函数本身</code></li>
<li>其作用是让某个构造函数产生的 所有实例对象（比如f） 能够找到他的构造函数（比如Fun），用法就是f.constructor</li>
</ul>
<ol start="2">
<li>此时实例对象里没有constructor 这个属性，于是沿着原型链往上找到Fun.prototype 里的constructor，并指向Fun 函数本身</li>
</ol>
<ul>
<li>constructor本就存在于原型中,指向构造函数,成为子对象后，如果该原型链中的constructor在自身没有而是在父原型中找到,所以指向父类的构造函数</li>
</ul>
<ol start="3">
<li>由于这里的继承是直接改了构造函数的prototype 的指向，所以在 sub的原型链中，Sub.prototype 没有constructor 属性，反而是看到了一个super 实例</li>
<li>这就让sub 实例的constructor 无法使用了。为了他还能用，就在那个super 实例中手动加了一个constructor 属性，且指向Sub 函数看到了一个super 实例</li>
</ol>
<h3 id="2-2-借用构造函数继承-假的"><a href="#2-2-借用构造函数继承-假的" class="headerlink" title="2.2 借用构造函数继承(假的)"></a>2.2 借用构造函数继承(假的)</h3><p>方式2: 借用构造函数继承(假的)</p>
<ol>
<li>套路:<ul>
<li>定义父类型构造函数</li>
<li>定义子类型构造函数</li>
<li>在子类型构造函数中调用父类型构造</li>
</ul>
</li>
<li>关键:<ul>
<li><code>在子类型构造函数中通用call()调用父类型构造函数</code></li>
</ul>
</li>
<li>作用:</li>
</ol>
<ul>
<li>能借用父类中的构造方法,但是不灵活</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params">name, age</span>) &#123;<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">age</span> = age<br>&#125;<br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Student</span>(<span class="hljs-params">name, age, price</span>) &#123;<br>  <span class="hljs-comment">//此处利用call(),将 [Student]的this传递给Person构造函数</span><br> <span class="hljs-title class_">Person</span>.<span class="hljs-title function_">call</span>(<span class="hljs-variable language_">this</span>, name, age)  <span class="hljs-comment">// 相当于: this.Person(name, age)</span><br> <span class="hljs-comment">/*this.name = name</span><br><span class="hljs-comment"> this.age = age*/</span><br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">price</span> = price<br>&#125;<br><br><span class="hljs-keyword">var</span> s = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Student</span>(<span class="hljs-string">&#x27;Tom&#x27;</span>, <span class="hljs-number">20</span>, <span class="hljs-number">14000</span>)<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(s.<span class="hljs-property">name</span>, s.<span class="hljs-property">age</span>, s.<span class="hljs-property">price</span>)<br></code></pre></td></tr></table></figure>

<p><strong>[<code>Person</code>]中的this是动态变化的,在[<code>Student</code>]中利用[<code>Person.call(this, name, age)</code>]改变了其this指向,所以可以实现此效果</strong></p>
<h3 id="2-3-组合继承"><a href="#2-3-组合继承" class="headerlink" title="2.3 组合继承"></a>2.3 组合继承</h3><p>方式3: 原型链+借用构造函数的组合继承</p>
<ol>
<li>利用原型链实现对父类型对象的方法继承</li>
<li>利用super()借用父类型构建函数初始化相同属性</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">function</span> <span class="hljs-title function_">Person</span>(<span class="hljs-params">name, age</span>) &#123;<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">age</span> = age<br>&#125;<br><span class="hljs-title class_">Person</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">setName</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">name</span>) &#123;<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">name</span> = name<br>&#125;<br><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">Student</span>(<span class="hljs-params">name, age, price</span>) &#123;<br> <span class="hljs-title class_">Person</span>.<span class="hljs-title function_">call</span>(<span class="hljs-variable language_">this</span>, name, age)  <span class="hljs-comment">// 为了得到属性</span><br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">price</span> = price<br>&#125;<br><span class="hljs-title class_">Student</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span> = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Person</span>() <span class="hljs-comment">// 为了能看到父类型的方法</span><br><span class="hljs-title class_">Student</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">constructor</span> = <span class="hljs-title class_">Student</span> <span class="hljs-comment">//修正constructor属性</span><br><span class="hljs-title class_">Student</span>.<span class="hljs-property"><span class="hljs-keyword">prototype</span></span>.<span class="hljs-property">setPrice</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">price</span>) &#123;<br> <span class="hljs-variable language_">this</span>.<span class="hljs-property">price</span> = price<br>&#125;<br><br><span class="hljs-keyword">var</span> s = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Student</span>(<span class="hljs-string">&#x27;Tom&#x27;</span>, <span class="hljs-number">24</span>, <span class="hljs-number">15000</span>)<br>s.<span class="hljs-title function_">setName</span>(<span class="hljs-string">&#x27;Bob&#x27;</span>)<br>s.<span class="hljs-title function_">setPrice</span>(<span class="hljs-number">16000</span>)<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(s.<span class="hljs-property">name</span>, s.<span class="hljs-property">age</span>, s.<span class="hljs-property">price</span>)<br></code></pre></td></tr></table></figure>

<h1 id="四、线程机制与事件机制"><a href="#四、线程机制与事件机制" class="headerlink" title="四、线程机制与事件机制"></a>四、线程机制与事件机制</h1><h3 id="1、进程与线程"><a href="#1、进程与线程" class="headerlink" title="1、进程与线程"></a>1、进程与线程</h3><h4 id="1-1-进程"><a href="#1-1-进程" class="headerlink" title="1.1 进程"></a>1.1 进程</h4><ol>
<li>程序的一次执行,它<code>占有一片独有的内存空间</code></li>
<li>可以通过windows任务管理器查看进程</li>
</ol>
<ul>
<li>可以看出每个程序的内存空间是相互独立的</li>
</ul>
<h4 id="1-2-线程"><a href="#1-2-线程" class="headerlink" title="1.2 线程"></a>1.2 线程</h4><p>概念:</p>
<ul>
<li>是进程内的一个独立执行单元</li>
<li>是程序执行的一个完整流程</li>
<li>是CPU的最小的调度单元</li>
</ul>
<h4 id="1-3-进程与线程"><a href="#1-3-进程与线程" class="headerlink" title="1.3 进程与线程"></a>1.3 进程与线程</h4><ol>
<li>应用程序必须运行在某个进程的某个线程上</li>
<li>一个进程中至少有一个运行的线程:主线程                 –&gt;进程启动后自动创建</li>
<li>一个进程中也可以同时运行多个线程:此时我们会说这个程序是多线程运行的</li>
<li>多个进程之间的数据是不能直接共享的                    –&gt;内存相互独立(隔离)</li>
<li><code>线程池(thread pool)</code>:保存多个线程对象的容器,实现线程对象的反复利用</li>
</ol>
<h4 id="1-4-相关的问题"><a href="#1-4-相关的问题" class="headerlink" title="1.4 相关的问题"></a>1.4 相关的问题</h4><h5 id="1-4-1-何为多进程与多线程"><a href="#1-4-1-何为多进程与多线程" class="headerlink" title="1.4.1 何为多进程与多线程?"></a>1.4.1 何为多进程与多线程?</h5><p>多进程运行: 一应用程序可以同时启动多个实例运行</p>
<p>多线程: 在一个进程内, 同时有多个线程运行</p>
<h5 id="1-4-2-比较单线程与多线程"><a href="#1-4-2-比较单线程与多线程" class="headerlink" title="1.4.2 比较单线程与多线程?"></a>1.4.2 比较单线程与多线程?</h5><p>多线程:</p>
<ul>
<li>优点:能有效提升CPU的利用率</li>
<li>缺点</li>
<li>创建多线程开销</li>
<li>线程间切换开销</li>
<li>死锁与状态同步问题</li>
</ul>
<p>单线程:</p>
<ul>
<li>优点:顺序编程简单易懂</li>
<li>缺点:效率低</li>
</ul>
<h5 id="1-4-3-JS是单线程还是多线程"><a href="#1-4-3-JS是单线程还是多线程" class="headerlink" title="1.4.3  JS是单线程还是多线程?"></a>1.4.3  JS是单线程还是多线程?</h5><p><code>JS是单线程运行的 , 但使用H5中的 Web Workers可以多线程运行</code></p>
<ul>
<li>只能由一个线程去操作DOM界面</li>
</ul>
<h5 id="1-4-4-浏览器运行是单线程还是多线程"><a href="#1-4-4-浏览器运行是单线程还是多线程" class="headerlink" title="1.4.4 浏览器运行是单线程还是多线程?"></a>1.4.4 浏览器运行是单线程还是多线程?</h5><p>都是多线程运行的</p>
<h5 id="1-4-5-浏览器运行是单进程还是多进程"><a href="#1-4-5-浏览器运行是单进程还是多进程" class="headerlink" title="1.4.5 浏览器运行是单进程还是多进程?"></a>1.4.5 浏览器运行是单进程还是多进程?</h5><p>有的是单进程:</p>
<ul>
<li>firefox</li>
<li>老版IE</li>
</ul>
<p>有的是多进程:</p>
<ul>
<li>chrome</li>
<li>新版IE</li>
</ul>
<p>如何查看浏览器是否是多进程运行的呢? 任务管理器–&gt;进程</p>
<h3 id="2、浏览器内核"><a href="#2、浏览器内核" class="headerlink" title="2、浏览器内核"></a>2、浏览器内核</h3><p>支撑浏览器运行的最核心的程序</p>
<h4 id="2-1-不同浏览器的内核"><a href="#2-1-不同浏览器的内核" class="headerlink" title="2.1 不同浏览器的内核"></a>2.1 不同浏览器的内核</h4><ul>
<li>Chrome, Safari : webkit</li>
<li>firefox : Gecko</li>
<li>IE    : Trident</li>
<li>360,搜狗等国内浏览器: Trident + webkit</li>
</ul>
<h4 id="2-2-内核由什么模块组成"><a href="#2-2-内核由什么模块组成" class="headerlink" title="2.2 内核由什么模块组成?"></a>2.2 内核由什么模块组成?</h4><p>主线程</p>
<ol>
<li>js引擎模块 : 负责js程序的编译与运行</li>
<li>html,css文档解析模块 : 负责页面文本的解析(拆解)</li>
<li>dom&#x2F;css模块 : 负责dom&#x2F;css在内存中的相关处理</li>
<li>布局和渲染模块 : 负责页面的布局和效果的绘制</li>
<li>布局和渲染模块 : 负责页面的布局和效果的绘制</li>
</ol>
<p>分线程</p>
<ul>
<li>定时器模块 : 负责定时器的管理</li>
<li>网络请求模块 : 负责服务器请求(常规&#x2F;Ajax)</li>
<li>事件响应模块 : 负责事件的管理</li>
</ul>
<h4 id="2-3-定时器引发的思考"><a href="#2-3-定时器引发的思考" class="headerlink" title="2.3 定时器引发的思考"></a>2.3 定时器引发的思考</h4><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs js">&lt;body&gt;<br><span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;btn&quot;</span>&gt;</span>启动定时器<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span><br><span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;</span><span class="language-javascript"></span></span><br><span class="language-javascript"><span class="language-xml"><span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">&#x27;btn&#x27;</span>).<span class="hljs-property">onclick</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) &#123;</span></span><br><span class="language-javascript"><span class="language-xml"><span class="hljs-keyword">var</span> start = <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>()</span></span><br><span class="language-javascript"><span class="language-xml"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;启动定时器前...&#x27;</span>)</span></span><br><span class="language-javascript"><span class="language-xml"><span class="hljs-built_in">setTimeout</span>(<span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) &#123;</span></span><br><span class="language-javascript"><span class="language-xml"> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;定时器执行了&#x27;</span>, <span class="hljs-title class_">Date</span>.<span class="hljs-title function_">now</span>()-start) <span class="hljs-comment">//定时器并不能保证真正定时执行,一般会延迟一丁点</span></span></span><br><span class="language-javascript"><span class="language-xml">&#125;, <span class="hljs-number">200</span>)</span></span><br><span class="language-javascript"><span class="language-xml"><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;启动定时器后...&#x27;</span>)</span></span><br><span class="language-javascript"><span class="language-xml"><span class="hljs-comment">// 做一个长时间的工作</span></span></span><br><span class="language-javascript"><span class="language-xml"><span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">1000000000</span>; i++) &#123; <span class="hljs-comment">//会造成定时器延长很长时间</span></span></span><br><span class="language-javascript"><span class="language-xml">   ...</span></span><br><span class="language-javascript"><span class="language-xml">&#125;</span></span><br><span class="language-javascript"><span class="language-xml">&#125;</span></span><br><span class="language-javascript"><span class="language-xml"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span><br>&lt;/body&gt;<br></code></pre></td></tr></table></figure>

<h5 id="2-3-1-定时器真是定时执行的吗"><a href="#2-3-1-定时器真是定时执行的吗" class="headerlink" title="2.3.1 定时器真是定时执行的吗?"></a>2.3.1 定时器真是定时执行的吗?</h5><ul>
<li>定时器并不能保证真正定时执行</li>
<li>一般会延迟一丁点(可以接受), 也有可能延迟很长时间(不能接受)</li>
</ul>
<h5 id="2-3-2-定时器回调函数是在分线程执行的吗"><a href="#2-3-2-定时器回调函数是在分线程执行的吗" class="headerlink" title="2.3.2 定时器回调函数是在分线程执行的吗?"></a>2.3.2 定时器回调函数是在分线程执行的吗?</h5><p>在主线程执行的, JS是单线程的</p>
<h5 id="2-3-3-定时器是如何实现的"><a href="#2-3-3-定时器是如何实现的" class="headerlink" title="2.3.3 定时器是如何实现的?"></a>2.3.3 定时器是如何实现的?</h5><p><code>事件循环模型</code></p>
<h3 id="3-JS是单线程的"><a href="#3-JS是单线程的" class="headerlink" title="3. JS是单线程的"></a>3. JS是单线程的</h3><h4 id="3-1-如何证明JS执行是单线程的"><a href="#3-1-如何证明JS执行是单线程的" class="headerlink" title="3.1 如何证明JS执行是单线程的"></a>3.1 如何证明JS执行是单线程的</h4><ul>
<li>setTimeout()的回调函数是在主线程执行的&#96;</li>
<li>定时器回调函数只有在运行栈中的代码全部执行完后才有可能执行</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 如何证明JS执行是单线程的</span><br><span class="hljs-built_in">setTimeout</span>(<span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) &#123; <span class="hljs-comment">//4. 在将[timeout 1111]弹窗关闭后,再等一秒 执行此处</span><br>   <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;timeout 2222&#x27;</span>)<br>   <span class="hljs-title function_">alert</span>(<span class="hljs-string">&#x27;22222222&#x27;</span>)<br> &#125;, <span class="hljs-number">2000</span>)<br> <span class="hljs-built_in">setTimeout</span>(<span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) &#123; <span class="hljs-comment">//3. 过了一秒后 打印 timeout 1111并弹窗,此处如果不将弹窗关闭,不会继续执行上方222</span><br>   <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;timeout 1111&#x27;</span>)<br>   <span class="hljs-title function_">alert</span>(<span class="hljs-string">&#x27;1111111&#x27;</span>)<br> &#125;, <span class="hljs-number">1000</span>)<br> <span class="hljs-built_in">setTimeout</span>(<span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) &#123; <span class="hljs-comment">//2. 然后打印timeout() 00000</span><br>   <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;timeout() 00000&#x27;</span>)<br> &#125;, <span class="hljs-number">0</span>)<br> <span class="hljs-keyword">function</span> <span class="hljs-title function_">fn</span>(<span class="hljs-params"></span>) &#123; <span class="hljs-comment">//1. fn()</span><br>   <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;fn()&#x27;</span>)<br> &#125;<br> <span class="hljs-title function_">fn</span>()<br><span class="hljs-comment">//----------------------</span><br> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;alert()之前&#x27;</span>)<br> <span class="hljs-title function_">alert</span>(<span class="hljs-string">&#x27;------&#x27;</span>) <span class="hljs-comment">//暂停当前主线程的执行, 同时暂停计时, 点击确定后, 恢复程序执行和计时</span><br> <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;alert()之后&#x27;</span>)<br></code></pre></td></tr></table></figure>

<p>流程结果:</p>
<ol>
<li>先打印了[<code>fn()</code>],然后马上就打印了[<code>timeout() 00000</code>]</li>
<li>过了一秒后 打印 timeout 1111并弹窗,此处如果不将弹窗关闭,不会继续执行上方222</li>
<li>在将[timeout 1111]弹窗关闭后,<code>再等一秒</code> 执行此处</li>
</ol>
<ul>
<li>问:为何明明写的是2秒,却关闭上一个弹窗再过一秒就执行?</li>
<li>解:并不是关闭后再计算的,而是一起计算的,alert只是暂停了主线程执行</li>
</ul>
<h4 id="3-2-JS引擎执行代码的基本流程与代码分类"><a href="#3-2-JS引擎执行代码的基本流程与代码分类" class="headerlink" title="3.2 JS引擎执行代码的基本流程与代码分类"></a>3.2 JS引擎执行代码的基本流程与代码分类</h4><p>代码分类:</p>
<ul>
<li>初始化代码</li>
<li>回调代码</li>
</ul>
<p>js引擎执行代码的基本流程</p>
<ol>
<li>先执行初始化代码: 包含一些特别的代码   回调函数(异步执行)</li>
</ol>
<ul>
<li>设置定时器</li>
<li>绑定事件监听</li>
<li>发送ajax请求</li>
</ul>
<ol start="2">
<li>后面在某个时刻才会执行回调代码</li>
</ol>
<h4 id="3-3-为什么js要用单线程模式-而不用多线程模式"><a href="#3-3-为什么js要用单线程模式-而不用多线程模式" class="headerlink" title="3.3 为什么js要用单线程模式, 而不用多线程模式?"></a>3.3 为什么js要用单线程模式, 而不用多线程模式?</h4><ol>
<li>avaScript的单线程，与它的用途有关。</li>
<li>作为浏览器脚本语言，JavaScript的主要用途是与用户互动，以及操作DOM。</li>
<li>这决定了它只能是单线程，否则会带来很复杂的同步问题<ul>
<li>举个栗子:如果我们要实现更新页面上一个dom节点然后删除,用单线程是没问题的</li>
<li>但是如果多线程,当我删除线程先删除了dom节点,更新线程要去更新的时候就会出错</li>
</ul>
</li>
</ol>
<h3 id="4、事件循环模型-Event-Loop-机制"><a href="#4、事件循环模型-Event-Loop-机制" class="headerlink" title="4、事件循环模型(Event Loop)机制"></a>4、事件循环模型(Event Loop)机制</h3><h4 id="4-1-概念引出"><a href="#4-1-概念引出" class="headerlink" title="4.1 概念引出"></a>4.1 概念引出</h4><p>我们都知道，<code>javascript从诞生之日起就是一门单线程的非阻塞的脚本语言</code>。这是由其最初的用途来决定的：<code>与浏览器交互</code>。</p>
<p>单线程意味着，javascript代码在执行的任何时候，都只有一个主线程来处理所有的任务。</p>
<p><code>非阻塞</code>:</p>
<blockquote>
<p>而非阻塞则是当代码需要进行一项异步任务（无法立刻返回结果，需要花一定时间才能返回的任务，如I&#x2F;O事件）的时候，主线程会挂起（pending）这个任务，然后在异步任务返回结果的时候再根据一定规则去执行相应的回调。</p>
</blockquote>
<p><code>单线程是必要的</code>:</p>
<blockquote>
<p>也是javascript这门语言的基石，原因之一在其最初也是最主要的执行环境——浏览器中，我们需要进行各种各样的dom操作。试想一下 如果javascript是多线程的，那么当两个线程同时对dom进行一项操作，例如一个向其添加事件，而另一个删除了这个dom，此时该如何处理呢？因此，为了保证不会 发生类似于这个例子中的情景，javascript选择只用一个主线程来执行代码，这样就保证了程序执行的一致性。</p>
</blockquote>
<p>当然，现如今人们也意识到，单线程在保证了执行顺序的同时也限制了javascript的效率，因此开发出了<code>web workers</code>技术。这项技术号称可以让javaScript成为一门多线程语言。</p>
<blockquote>
<p>然而，使用web workers技术开的多线程有着诸多限制，例如：<code>所有新线程都受主线程的完全控制，不能独立执行</code>。这意味着这些“线程” 实际上应属于主线程的子线程。另外，这些子线程并没有执行I&#x2F;O操作的权限，只能为主线程分担一些诸如计算等任务。所以严格来讲这些线程并没有完整的功能，也因此这项技术并非改变了javascript语言的单线程本质。</p>
</blockquote>
<p>可以预见，未来的javascript也会一直是一门单线程的语言。</p>
<p>话说回来，前面提到javascript的另一个特点是“<code>非阻塞</code>”，那么javascript引擎到底是如何实现的这一点呢？</p>
<blockquote>
<p>答案就是——event loop（事件循环）。</p>
</blockquote>
<p><strong>注：<code>虽然nodejs中的也存在与传统浏览器环境下的相似的事件循环。然而两者间却有着诸多不同，故把两者分开，单独解释</code>。</strong></p>
<h4 id="4-2-浏览器环境下JS引擎的事件循环机制"><a href="#4-2-浏览器环境下JS引擎的事件循环机制" class="headerlink" title="4.2 浏览器环境下JS引擎的事件循环机制"></a>4.2 浏览器环境下JS引擎的事件循环机制</h4><h5 id="4-2-1-执行栈概念"><a href="#4-2-1-执行栈概念" class="headerlink" title="4.2.1 执行栈概念"></a>4.2.1 执行栈概念</h5><p>执行上下文栈详情可以看上方笔记、执行上下文与执行上下文栈),此处继续进行一次概述加深理解</p>
<p>当javascript代码执行的时候会将不同的变量存于内存中的不同位置：<code>堆（heap）</code>和<code>栈（stack）</code>中来加以区分。其中，堆里存放着一些对象。而栈中则存放着一些基础类型变量以及对象的指针。 <code>但是我们这里说的执行栈和上面这个栈的意义却有些不同</code>。</p>
<p><code>执行栈</code>:</p>
<blockquote>
<p>当我们调用一个方法的时候，js会生成一个与这个方法对应的执行环境（context），又叫<code>执行上下文</code>。这个执行环境中存在着这个方法的私有作用域、上层作用域的指向、方法的参数，这个作用域中定义的变量以及这个作用域的this对象。 而当一系列方法被依次调用的时候，因为js是单线程的，同一时间只能执行一个方法，于是这些方法被排队在一个单独的地方。这个地方被称为执行栈。</p>
</blockquote>
<p>当一个脚本第一次执行的时候，js引擎会解析这段代码，并将其中的同步代码按照执行顺序加入执行栈中，然后从头开始执行。如果当前执行的是一个方法，那么js会向执行栈中添加这个方法的执行环境，然后进入这个执行环境继续执行其中的代码。<code>当这个执行环境中的代码 执行完毕并返回结果后，js会退出这个执行环境并把这个执行环境销毁，回到上一个方法的执行环境</code>。这个过程反复进行，直到执行栈中的代码全部执行完毕。</p>
<p>以上的过程说的都是同步代码的执行。那么当一个异步代码（如发送ajax请求数据）执行后会如何呢？</p>
<blockquote>
<p>刚刚说过js的另一大特点是非阻塞，实现这一点的关键在于下面要说的这项机制——<code>事件队列（Task Queue）</code>。</p>
</blockquote>
<h5 id="4-2-2-事件队列（Task-Queue）"><a href="#4-2-2-事件队列（Task-Queue）" class="headerlink" title="4.2.2 事件队列（Task Queue）"></a>4.2.2 事件队列（Task Queue）</h5><p>JS引擎遇到一个异步事件后并不会一直等待其返回结果，而是会将这个事件挂起，继续执行执行栈中的其他任务,当一个异步事件返回结果后，js会将这个事件加入与当前执行栈不同的另一个队列，我们称之为<code>事件队列</code>。</p>
<p>被放入事件队列不会立刻执行其回调，而是<code>等待当前执行栈中的所有任务都执行完毕， 主线程处于闲置状态时，主线程会去查找事件队列是否有任务</code>。如果有，那么主线程会从中取出排在第一位的事件，并把这个事件对应的回调放入执行栈中，然后执行其中的同步代码…，如此反复，<code>这样就形成了一个无限的循环。这就是这个过程被称为“事件循环（Event Loop）”的原因。</code></p>
<h4 id="4-3-宏任务-macro-task-与微任务-micro-task"><a href="#4-3-宏任务-macro-task-与微任务-micro-task" class="headerlink" title="4.3 宏任务(macro task)与微任务(micro task)"></a>4.3 宏任务(<strong>macro task</strong>)与微任务(<strong>micro task</strong>)</h4><p>顾名思义,宏任务放至宏任务队列(<code>简称宏队列</code>)中、微任务放至微任务队列(<code>简称微队列</code>)中</p>
<ol>
<li>JS中用来存储待执行回调函数的队列包含2个不同特定的列队</li>
</ol>
<ul>
<li><code>宏队列</code>:用来保存待执行的宏任务(回调),比如:<code>定时器</code>回调&#x2F;ajax回调&#x2F;dom事件回调</li>
<li><code>微队列</code>:用来保存待执行的微任务(回调),比如:<code>Promise</code>的回调&#x2F;muntation回调</li>
</ul>
<ol start="2">
<li>JS执行时会区别这2个队列:<ul>
<li>JS执行引擎首先必须执行所有的<code>初始化同步任务</code>代码</li>
<li>每次准备取出第一个<code>宏任务执行前</code>,都要将所有的<code>微任务</code>一个一个取出来执行</li>
</ul>
</li>
</ol>
<p>前面我们介绍过，在一个事件循环中，异步事件返回结果后会被放到一个任务队列中。然而，根据这个异步事件的类型，这个事件实际上会被对应的宏任务队列或者微任务队列中去。并且在当前执行栈为空的时候，主线程会 查看微任务队列是否有事件存在。如果不存在，那么再去宏任务队列中取出一个事件并把对应的回到加入当前执行栈；如果存在，则会依次执行队列中事件对应的回调，直到微任务队列为空，然后去宏任务队列中取出最前面的一个事件，把对应的回调加入当前执行栈…如此反复，进入循环。</p>
<p>我们只需记住:** <code>当前执行栈执行完毕时会立刻先处理所有微任务队列中的事件，然后再去宏任务队列中取出一个事件。同一次事件循环中，微任务永远在宏任务之前执行</code> **</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123; <br>   <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;timeout callback1（）&#x27;</span>)<span class="hljs-comment">//立即放入宏队列</span><br>   <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>(<span class="hljs-number">3</span>).<span class="hljs-title function_">then</span>(<br>     <span class="hljs-function"><span class="hljs-params">value</span> =&gt;</span> &#123; <br>       <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;Promise onResolved3()&#x27;</span>, value)<span class="hljs-comment">//当这个宏任务执行后 立马放入微队列,所以这个微任务执行完后下个宏任务才能执行 </span><br>     &#125;<br>   )<br> &#125;, <span class="hljs-number">0</span>)<br><br> <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123; <br>   <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;timeout callback2（）&#x27;</span>) <span class="hljs-comment">//立即放入宏队列,</span><br> &#125;, <span class="hljs-number">0</span>)<br><br> <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>(<span class="hljs-number">1</span>).<span class="hljs-title function_">then</span>(<br>   <span class="hljs-function"><span class="hljs-params">value</span> =&gt;</span> &#123; <br>     <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;Promise onResolved1()&#x27;</span>, value)<span class="hljs-comment">//立即放入微队列</span><br>     <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>       <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;timeout callback3（）&#x27;</span>, value) <span class="hljs-comment">//立即放入宏任务</span><br>     &#125;, <span class="hljs-number">0</span>)<br>   &#125;<br> )<br><br> <span class="hljs-title class_">Promise</span>.<span class="hljs-title function_">resolve</span>(<span class="hljs-number">2</span>).<span class="hljs-title function_">then</span>(<br>   <span class="hljs-function"><span class="hljs-params">value</span> =&gt;</span> &#123; <br>     <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;Promise onResolved2()&#x27;</span>, value)<span class="hljs-comment">//立即放入微队列</span><br>   &#125;<br> )<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;同步代码&#x27;</span>) <span class="hljs-comment">//同步代码立即执行</span><br></code></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-string">&#x27;同步代码&#x27;</span>,<br><span class="hljs-string">&#x27;Promise onResolved1()&#x27;</span>,<br><span class="hljs-string">&#x27;Promise onResolved2()&#x27;</span>,<br><span class="hljs-string">&#x27;timeout callback1（）&#x27;</span>,<br><span class="hljs-string">&#x27;Promise onResolved3()&#x27;</span>,<br><span class="hljs-string">&#x27;timeout callback2（）&#x27;</span>,<br><span class="hljs-string">&#x27;timeout callback3（）&#x27;</span><br></code></pre></td></tr></table></figure>

<h4 id="4-4-setTimeout-和setImmediate"><a href="#4-4-setTimeout-和setImmediate" class="headerlink" title="4.4 setTimeout()和setImmediate()"></a>4.4 setTimeout()和setImmediate()</h4><p>在三个方法中，这两个方法最容易被弄混。实际上，某些情况下这两个方法的表现也非常相似。然而实际上，这两个方法的意义却大为不同。</p>
<p><code>setTimeout()</code>方法是定义一个回调，并且希望这个回调在我们所指定的时间间隔后第一时间去执行。注意这个“第一时间执行”，这意味着，受到操作系统和当前执行任务的诸多影响，该回调并不会在我们预期的时间间隔后精准的执行。执行的时间存在一定的延迟和误差，这是不可避免的。node会在可以执行timer回调的第一时间去执行你所设定的任务。</p>
<p><code>setImmediate()</code>方法从意义上将是立刻执行的意思，但是实际上它却是在一个固定的阶段才会执行回调，即poll阶段之后。有趣的是，这个名字的意义和之前提到过的<code>process.nextTick()</code>方法才是最匹配的。node的开发者们也清楚这两个方法的命名上存在一定的混淆，他们表示不会把这两个方法的名字调换过来—因为有大量的node程序使用着这两个方法，调换命名所带来的好处与它的影响相比不值一提。</p>
<p><code>setTimeout()</code>和不设置时间间隔的<code>setImmediate()</code>表现上及其相似。猜猜下面这段代码的结果是什么？</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;timeout&#x27;</span>);<br>&#125;, <span class="hljs-number">0</span>);<br><br><span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =&gt;</span> &#123;<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;immediate&#x27;</span>);<br>&#125;);<br></code></pre></td></tr></table></figure>

<p>实际上，答案是不一定。没错，就连node的开发者都无法准确的判断这两者的顺序谁前谁后。这取决于这段代码的运行环境。运行环境中的各种复杂的情况会导致在同步队列里两个方法的顺序随机决定。但是，在一种情况下可以准确判断两个方法回调的执行顺序，那就是在一个I&#x2F;O事件的回调中。下面这段代码的顺序永远是固定的：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&#x27;fs&#x27;</span>);<br><br>fs.<span class="hljs-title function_">readFile</span>(__filename, <span class="hljs-function">() =&gt;</span> &#123;<br><span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>   <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;timeout&#x27;</span>);<br>&#125;, <span class="hljs-number">0</span>);<br><span class="hljs-title function_">setImmediate</span>(<span class="hljs-function">() =&gt;</span> &#123;<br>   <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;immediate&#x27;</span>);<br>&#125;);<br>&#125;);<br></code></pre></td></tr></table></figure>

<p>答案永远是：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs js">immediate<br>timeout<br></code></pre></td></tr></table></figure>

<p>因为在I&#x2F;O事件的回调中，setImmediate方法的回调永远在timer的回调前执行。</p>
<h3 id="5-Web-Workers"><a href="#5-Web-Workers" class="headerlink" title="5. Web Workers"></a>5. Web Workers</h3><p>想了解更多可以点击链接查看更多,此处只是大致了解学习   –&gt;<a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Workers_API/Using_web_workers">Web Workers</a></p>
<ol>
<li>H5规范提供了js分线程的实现, 取名为: Web Workers</li>
<li>相关API</li>
</ol>
<ul>
<li>Worker: 构造函数, 加载分线程执行的js文件</li>
<li>Worker.prototype.onmessage: 用于接收另一个线程的回调函数</li>
<li>Worker.prototype.postMessage: 向另一个线程发送消息</li>
</ul>
<ol start="3">
<li>不足</li>
</ol>
<ul>
<li>worker内代码不能操作DOM(更新UI)</li>
<li>不能跨域加载JS</li>
<li>不是每个浏览器都支持这个新特性</li>
</ul>
<h4 id="5-1-抛砖引玉-引出用处"><a href="#5-1-抛砖引玉-引出用处" class="headerlink" title="5.1 抛砖引玉,引出用处"></a>5.1 抛砖引玉,引出用处</h4><p>斐波那契（Fibonacci）数列来做例子,这东西效率低,可以拿来模拟</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs js">&lt;body&gt;<br><span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">&quot;数值&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;number&quot;</span>&gt;</span></span><br><span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;btn&quot;</span>&gt;</span>计算<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span><br><span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;</span><span class="language-javascript"></span></span><br><span class="language-javascript"><span class="language-xml"><span class="hljs-comment">// 1 1 2 3 5 8    f(n) = f(n-1) + f(n-2)</span></span></span><br><span class="language-javascript"><span class="language-xml"><span class="hljs-keyword">function</span> <span class="hljs-title function_">fibonacci</span>(<span class="hljs-params">n</span>) &#123;</span></span><br><span class="language-javascript"><span class="language-xml"><span class="hljs-keyword">return</span> n&lt;=<span class="hljs-number">2</span> ? <span class="hljs-number">1</span> : <span class="hljs-title function_">fibonacci</span>(n-<span class="hljs-number">1</span>) + <span class="hljs-title function_">fibonacci</span>(n-<span class="hljs-number">2</span>)  <span class="hljs-comment">//递归调用</span></span></span><br><span class="language-javascript"><span class="language-xml">&#125;</span></span><br><span class="language-javascript"><span class="language-xml"><span class="hljs-comment">// console.log(fibonacci(7))</span></span></span><br><span class="language-javascript"><span class="language-xml"><span class="hljs-keyword">var</span> input = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">&#x27;number&#x27;</span>)</span></span><br><span class="language-javascript"><span class="language-xml"><span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">&#x27;btn&#x27;</span>).<span class="hljs-property">onclick</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) &#123;</span></span><br><span class="language-javascript"><span class="language-xml"><span class="hljs-keyword">var</span> number = input.<span class="hljs-property">value</span></span></span><br><span class="language-javascript"><span class="language-xml"><span class="hljs-keyword">var</span> result = <span class="hljs-title function_">fibonacci</span>(number)</span></span><br><span class="language-javascript"><span class="language-xml"><span class="hljs-title function_">alert</span>(result)</span></span><br><span class="language-javascript"><span class="language-xml">&#125;</span></span><br><span class="language-javascript"><span class="language-xml"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span><br></code></pre></td></tr></table></figure>

<p>当我运行此行代码,传入计算数值为50左右(有的甚至更低),整个页面就会卡住好久的时间不能操作(计算结束后才会弹窗,但是未弹窗的这段时间用户并不能进行操作),这时候就会发现单线程的弊端了</p>
<h4 id="5-2-尝试使用"><a href="#5-2-尝试使用" class="headerlink" title="5.2 尝试使用"></a>5.2 尝试使用</h4><ol>
<li>H5规范提供了js分线程的实现, 取名为: Web Workers</li>
<li>相关API</li>
</ol>
<ul>
<li>Worker: 构造函数, 加载分线程执行的js文件</li>
<li>Worker.prototype.onmessage: 用于接收另一个线程的回调函数</li>
<li>Worker.prototype.postMessage: 向另一个线程发送消息</li>
</ul>
<ol start="3">
<li>不足</li>
</ol>
<ul>
<li>worker内代码不能操作DOM(更新UI)</li>
<li>不能跨域加载JS</li>
<li>不是每个浏览器都支持这个新特性</li>
</ul>
<h5 id="5-2-1-主线程"><a href="#5-2-1-主线程" class="headerlink" title="5.2.1 主线程"></a>5.2.1 主线程</h5><ol>
<li>创建一个Worker对象</li>
<li>绑定[主线程接收分线程返回的数据]方法</li>
<li>主线程向分线程发送数据,然后等待接受数据</li>
<li>接收到分线程回馈的数据,将数据进行处理(如弹窗)</li>
</ol>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><code class="hljs js">&lt;body&gt;<br><span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">placeholder</span>=<span class="hljs-string">&quot;数值&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;number&quot;</span>&gt;</span></span><br><span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;btn&quot;</span>&gt;</span>计算<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span></span><br><span class="language-xml"><span class="hljs-tag">&lt;<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text/javascript&quot;</span>&gt;</span><span class="language-javascript"></span></span><br><span class="language-javascript"><span class="language-xml"> <span class="hljs-keyword">var</span> input = <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">&#x27;number&#x27;</span>)</span></span><br><span class="language-javascript"><span class="language-xml"> <span class="hljs-variable language_">document</span>.<span class="hljs-title function_">getElementById</span>(<span class="hljs-string">&#x27;btn&#x27;</span>).<span class="hljs-property">onclick</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) &#123;</span></span><br><span class="language-javascript"><span class="language-xml">   <span class="hljs-keyword">var</span> number = input.<span class="hljs-property">value</span></span></span><br><span class="language-javascript"><span class="language-xml"></span></span><br><span class="language-javascript"><span class="language-xml">   <span class="hljs-comment">//创建一个Worker对象</span></span></span><br><span class="language-javascript"><span class="language-xml">   <span class="hljs-keyword">var</span> worker = <span class="hljs-keyword">new</span> <span class="hljs-title class_">Worker</span>(<span class="hljs-string">&#x27;worker.js&#x27;</span>)</span></span><br><span class="language-javascript"><span class="language-xml">   <span class="hljs-comment">// 绑定接收消息的监听</span></span></span><br><span class="language-javascript"><span class="language-xml">   worker.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>) &#123; <span class="hljs-comment">//此处变成回调代码,会在初始化工作完成后才会进行</span></span></span><br><span class="language-javascript"><span class="language-xml">     <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;主线程接收分线程返回的数据: &#x27;</span>+event.<span class="hljs-property">data</span>)</span></span><br><span class="language-javascript"><span class="language-xml">     <span class="hljs-title function_">alert</span>(event.<span class="hljs-property">data</span>)</span></span><br><span class="language-javascript"><span class="language-xml">   &#125;</span></span><br><span class="language-javascript"><span class="language-xml"></span></span><br><span class="language-javascript"><span class="language-xml">   <span class="hljs-comment">// 向分线程发送消息</span></span></span><br><span class="language-javascript"><span class="language-xml">   worker.<span class="hljs-title function_">postMessage</span>(number)</span></span><br><span class="language-javascript"><span class="language-xml">   <span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;主线程向分线程发送数据: &#x27;</span>+number)</span></span><br><span class="language-javascript"><span class="language-xml"> &#125;</span></span><br><span class="language-javascript"><span class="language-xml"> <span class="hljs-comment">// console.log(this) // window</span></span></span><br><span class="language-javascript"><span class="language-xml"></span></span><br><span class="language-javascript"><span class="language-xml"></span><span class="hljs-tag">&lt;/<span class="hljs-name">script</span>&gt;</span></span><br><span class="language-xml"><span class="hljs-tag">&lt;/<span class="hljs-name">body</span>&gt;</span></span><br></code></pre></td></tr></table></figure>

<h5 id="5-2-2-分线程"><a href="#5-2-2-分线程" class="headerlink" title="5.2.2 分线程"></a>5.2.2 分线程</h5><p>将计算放置分线程中</p>
<p><code>注意</code>:alert(result)  alert是window的方法, 在分线程不能调用,<code>分线程中的全局对象不再是window</code>, 所以在分线程中不可能更新界面</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">//worker.js</span><br><span class="hljs-keyword">function</span> <span class="hljs-title function_">fibonacci</span>(<span class="hljs-params">n</span>) &#123;<br><span class="hljs-keyword">return</span> n&lt;=<span class="hljs-number">2</span> ? <span class="hljs-number">1</span> : <span class="hljs-title function_">fibonacci</span>(n-<span class="hljs-number">1</span>) + <span class="hljs-title function_">fibonacci</span>(n-<span class="hljs-number">2</span>)  <span class="hljs-comment">//递归调用</span><br>&#125;<br><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-variable language_">this</span>)<br><span class="hljs-variable language_">this</span>.<span class="hljs-property">onmessage</span> = <span class="hljs-keyword">function</span> (<span class="hljs-params">event</span>) &#123;<br><span class="hljs-keyword">var</span> number = event.<span class="hljs-property">data</span><br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;分线程接收到主线程发送的数据: &#x27;</span>+number)<br><span class="hljs-comment">//计算</span><br><span class="hljs-keyword">var</span> result = <span class="hljs-title function_">fibonacci</span>(number)<br><span class="hljs-title function_">postMessage</span>(result)<br><span class="hljs-variable language_">console</span>.<span class="hljs-title function_">log</span>(<span class="hljs-string">&#x27;分线程向主线程返回数据: &#x27;</span>+result)<br><span class="hljs-comment">// alert(result)  alert是window的方法, 在分线程不能调用</span><br><span class="hljs-comment">// 分线程中的全局对象不再是window, 所在分线程中不可能更新界面</span><br>&#125;<br></code></pre></td></tr></table></figure>












































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
              </div>
              <p class="note note-warning">
                <strong>本文作者: </strong><a href="/HexoPage/">xiao_aodi</a> <br>
                <!-- <strong>本文链接: </strong><a href="http://xiao_aodi.gitee.io/hexopage/2022/03/23/JavaScript%E5%AD%A6%E4%B9%A0%E5%B7%A9%E5%9B%BA%E5%9F%BA%E7%A1%80%E5%A4%96%E5%8A%A0%E8%BF%9B%E9%98%B6(%E4%B8%8B)%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%86/">http://xiao_aodi.gitee.io/hexopage/2022/03/23/JavaScript%E5%AD%A6%E4%B9%A0%E5%B7%A9%E5%9B%BA%E5%9F%BA%E7%A1%80%E5%A4%96%E5%8A%A0%E8%BF%9B%E9%98%B6(%E4%B8%8B)%E5%BA%95%E5%B1%82%E5%8E%9F%E7%90%86/</a> <br> -->
                <strong>版权声明: </strong>本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                   </p>
                   

              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/HexoPage/2022/03/23/Ajax%E8%AE%B0%E5%BD%95(%E8%AF%A6%E8%A7%A3)/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">Ajax学习记录(详解)</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/HexoPage/2022/03/22/JavaScript%E8%BF%9B%E9%98%B6%E5%AD%A6%E4%B9%A0%E7%9A%84%E7%AC%94%E8%AE%B0/">
                        <span class="hidden-mobile">JavaScript学习巩固基础外加进阶(上)底层原理</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  
   <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a>  
  <!-- 添加网站运行时间 -->
  <div class="footer-content">
    <div>
      <span id="timeDate">载入天数...</span>
      <span id="times">载入时分秒...</span>
      <script>
      var now = new Date();
      function createtime(){
          var grt= new Date("03/10/2022 00:00:00");//此处修改你的建站时间或者网站上线时间
          now.setTime(now.getTime()+250);
          days = (now - grt ) / 1000 / 60 / 60 / 24;
          dnum = Math.floor(days);
          hours = (now - grt ) / 1000 / 60 / 60 - (24 * dnum);
          hnum = Math.floor(hours);
          if(String(hnum).length ==1 ){
              hnum = "0" + hnum;
          }
          minutes = (now - grt ) / 1000 /60 - (24 * 60 * dnum) - (60 * hnum);
          mnum = Math.floor(minutes);
          if(String(mnum).length ==1 ){
                    mnum = "0" + mnum;
          }
          seconds = (now - grt ) / 1000 - (24 * 60 * 60 * dnum) - (60 * 60 * hnum) - (60 * mnum);
          snum = Math.round(seconds);
          if(String(snum).length ==1 ){
                    snum = "0" + snum;
          }
          document.getElementById("timeDate").innerHTML = "🚀 &nbsp"+dnum+"&nbsp天";  //此次自定义显示内容
          document.getElementById("times").innerHTML = hnum + "&nbsp小时&nbsp" + mnum + "&nbsp分&nbsp" + snum + "&nbsp秒";
      }  //此次自定义显示内容
      setInterval("createtime()",250);
      </script>
    </div>
  </div>
  <!-- 统计访问量信息 -->
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  <!-- 个人和备案信息 -->
  <div>©2022-2023<a target="_blank" rel="noopener" href="https://clearlight.blog.csdn.net/">ClearlightY</a> <a target="_blank" rel="noopener" href="http://beian.miit.gov.cn/">冀ICP备19019307号-2</a></div>
   
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        京ICP证123456号
      </a>
    </span>
    
      
        <span>
          <a
            href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=12345678"
            rel="nofollow noopener"
            class="beian-police"
            target="_blank"
          >
            
              <span style="visibility: hidden; width: 0">|</span>
              <img src="/HexoPage/img/police_beian.png" alt="police-icon"/>
            
            <span>京公网安备12345678号</span>
          </a>
        </span>
      
    
  </div>


  
</footer>

<!-- SCRIPTS -->

  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/HexoPage/js/events.js" ></script>
<script  src="/HexoPage/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/HexoPage/js/local-search.js" ></script>





  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/HexoPage/js/boot.js" ></script>




  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/HexoPage/js/events.js" ></script>
<script  src="/HexoPage/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/HexoPage/js/local-search.js" ></script>





  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/HexoPage/js/boot.js" ></script>


</body>
</html>
